/*
 * @(#)ItemTag.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.web.taglibs.pager;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @author Web Platform Development Team
 * @jsp.tag name="item" body-content="JSP"
 * @deprecated backward portability
 */
public class ItemTag extends TagSupport {
	static final long serialVersionUID = 1837066147585281099L;
	private String key;

	/**
	 * {@inheritDoc}
	 * */
	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException {
		try {
			ListTag listTag = getParentListTag(this);

			if (listTag == null) {
				listTag = new ListTag();
			}
			if (listTag.item == null) {
				listTag.item = new ConcurrentHashMap();
			}

			pageContext.getOut().print(listTag.item.get(key));
		} catch (IOException e) {
			throw new JspException(e.getMessage(), e);
		}
		return EVAL_PAGE;
	}

	/**
	 * @jsp.attribute required="true" rtexprvalue="true"
	 * @return key
	 */
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	/**
	 * @param current {@link Tag}
	 * @return {@link ListTag}
	 * */
	private ListTag getParentListTag(Tag current) {
		if (current.getParent() == null) {
			return null;
		}
		
		if (current.getParent() instanceof ListTag) {
			return (ListTag)current.getParent();
		}
		
		return getParentListTag(current.getParent());
	}
}
